package com.atguigu.tingshu.payment.api;

import com.atguigu.tingshu.common.anno.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.payment.service.WxPayService;
import com.atguigu.tingshu.vo.payment.WeChatPayVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "微信支付接口")
@RestController
@RequestMapping("api/payment/wxPay")
@Slf4j
public class WxPayApiController {

    @Autowired
    private WxPayService wxPayService;

    //微信下单结接口
    @GuiguLogin
    @PostMapping(value = "/createJsapi/{paymentType}/{orderNo}")
    public Result<WeChatPayVo> createJsapi(@PathVariable(value = "paymenType")String paymentType,@PathVariable(value = "orderNo") String orderNo){
        WeChatPayVo weChatPayVo = wxPayService.createJsapi(paymentType,orderNo);
        return Result.ok(weChatPayVo);
    }


    @GetMapping(value = "/queryPayStatus/{orderNo}")
    public Result<Boolean> queryPayStatus(@PathVariable(value = "orderNo") String orderNo) {
        boolean result =  wxPayService.queryPayStatus(orderNo) ;
        return Result.ok(result) ;
    }


    @PostMapping(value = "/notify")
    public ResponseEntity asyncNotify(HttpServletRequest request) {    // 当前接口的返回值中需要包含两部分的数据: http的状态码、响应体数据

        log.info("WxPayApiController...asyncNotify...方法执行了....");

        // 创建Map封装响应结果数据
        Map<String , String> body = new HashMap<>() ;

        try {

            // 调用服务层接口方法，进行业务逻辑的处理
            wxPayService.asyncNotify(request) ;
            body.put("code" , "SUCCESS") ;
            body.put("message" , "成功") ;

            return ResponseEntity.status(HttpStatus.OK).body(body) ;

        }catch (Exception e) {
            e.printStackTrace();
            body.put("code" , "FAIL") ;
            body.put("message" , "失败") ;
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(body) ;
        }

    }


}
